<?php
// +----------------------------------------------------------------------
// | Demo [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.lmx0536.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: limx <715557344@qq.com> <http://www.lmx0536.cn>
// +----------------------------------------------------------------------

namespace Home\Model;

use Think\Model;

/**
 * 模型模版
 */
class AdminModel extends Model
{
    //表名
    protected $tableName = 'admin';

    /**
     * 文件模型自动完成
     * @var array
     */
    protected $_auto = array(//array('create_time', 'fnGetNowTime', self::MODEL_INSERT, 'function'),
    );

    protected $_validate = array(
        //array('verify', 'require', '验证码必须！'), //默认情况下用正则进行验证
        //array('name', '', '帐号名称已经存在！', 0, 'unique', 1), // 在新增的时候验证name字段是否唯一
        //array('value', array(1, 2, 3), '值的范围不正确！', 2, 'in'), // 当值不为空的时候判断是否在一个范围内
        //array('repassword', 'password', '确认密码不正确', 0, 'confirm'), // 验证确认密码是否和密码一致
        //array('password', 'checkPwd', '密码格式不正确', 0, 'function'), // 自定义函数验证密码格式
    );

    /**
     * 删除
     */
    public function mfnDelete($id)
    {

        $condition["Id"] = $id;
        $res = $this->where($condition)->delete();
        if ($res) {
            return true;
        }

        return false;

    }

    /**
     * [mfnAdd 添加、编辑]
     * @Author   Limx
     * @Method   直接调用
     * @DateTime 2016-02-02T09:54:54+0800
     * @param    [type]                   $d    [所需的数据]
     * @return   [type]                         [description]
     */
    public function mfnUpdate($d)
    {
        $data = $this->create($d);

        if (empty($data)) {
            return false;
        }

        if (empty($data['Id'])) {
            $id = $this->add();
            if (!$id) {
                $this->error = '新增出错！';
                return false;
            }
        } else {
            $status = $this->save();
            if (false === $status) {
                $this->error = '更新出错！';
                return false;
            }
            $id = $data['Id'];
        }

        return $id;
    }

    /**
     * [mfnFind 获取一条信息]
     * @Author   Limx
     * @Method   直接调用
     * @DateTime 2016-02-02T09:54:54+0800
     * @param    [type]              $condition [筛选条件]
     * @return   [type]                         [description]
     */
    public function mfnFind($condition, $field = "")
    {
        $res = $this->field($field)->where($condition)->find();

        return $res;
    }

    /**
     * [mfnSelect 获取列表]
     * @Author   Limx
     * @Method   直接调用
     * @DateTime 2016-02-02T09:54:54+0800
     * @param    [type]              $condition [筛选条件]
     * @return   [type]                         [description]
     */
    public function mfnSelect($condition, $pageIndex = 0, $pageSize = 10, $order = "Id desc", $field = "")
    {
        $start = $pageIndex * $pageSize;
        $result = $this->field($field)
            ->where($condition)
            ->order($order)
            ->limit("$start,$pageSize")
            ->select();
        $count = $this->where($condition)->count();
        $returnData["data"] = $result;
        $returnData["count"] = $count;
        return $returnData;
    }

    public function mfnLogin($data)
    {
        if (isset($data["username"]) && isset($data["password"])) {
            $data["password"] = MD5($data["password"]);
            $res = $this->mfnFind($data);
            if ($res) {
                //登录
                session("UserID", $res["id"]);
                session("UserName", $res["name"]);
                session("Auth", $res["auth"]);
                return true;
            }
            $this->error = "用户名或密码错误！";
        } else {
            $this->error = "参数错误";
        }
        return false;
    }

}
